package com.wjy.common.mq.pc;

import com.wjy.common.bean.info.MailInfo;
import com.wjy.common.bean.info.SmsInfo;
import com.wjy.common.thread.create.ThreadPoolCreate;
import com.wjy.common.util.JacksonUtil;
import com.wjy.common.util.MailUtil;
import com.wjy.common.util.PropertiesUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 生产者/消费者 监听
 */
@Log4j2
@Component
public class JedisPusPopListener {

    private static ThreadPoolExecutor executor = ThreadPoolCreate.getExecutorSynchronousQueue();
    @Autowired
    private Poper poper;
    @Autowired
    private MailUtil mailUtil;
    @Autowired
    private JacksonUtil jacksonUtil;
    @Autowired
    private PropertiesUtil propertiesUtil;

    public void onMessage(String... keys) {

        String k1 = keys[0];
        String k2 = keys[1];

        log.info(k1 + " 已启动");
        log.info(k2 + " 已启动");

        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    String message = poper.pop(0, k1);
                    log.info(k1 + ": " + message);
                    MailInfo mailInfo = jacksonUtil.toEntity(message, MailInfo.class);
                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            try {
                                mailUtil.sendSimpleMail(propertiesUtil.getProperty("mail.from", String.class, null), mailInfo.getRecipientAddress(), "验证码", mailInfo.getText());
                            } catch (Exception e) {
                                log.error(e);
                            }
                        }
                    };
                    executor.execute(runnable);
                }
            }
        };

        Runnable r2 = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    String message = poper.pop(0, k2);
                    log.info(k2 + ": " + message);
                    SmsInfo smsInfo = jacksonUtil.toEntity(message, SmsInfo.class);
                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            try {
                                mailUtil.sendSimpleMail(propertiesUtil.getProperty("mail.from", String.class, null), smsInfo.getRecipientAddress(), "验证码", smsInfo.getText());
                            } catch (Exception e) {
                                log.error(e);
                            }
                        }
                    };
                    executor.execute(runnable);
                }
            }
        };

        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);

        t1.start();
        t2.start();

    }

}
